<template>
  <div class="comment-list flex-direction-column">
    <p class="title" v-if="comments.length">评论列表</p>
    <div class="list-item" v-for="item in comments" :key="item._id">
      <div class="desc">
        <div class="user display-flex">
          <img :src="item.uid?.avatar_img" width="30" height="30" alt="" />
          <div class="info">
            <a href="#" class="name">{{ item.uid?.nikname }}</a>
            <p class="content">{{ item.content }}</p>
          </div>
        </div>
        <p class="time">{{ item.date | parseTimeTz }}</p>
      </div>
      <el-popconfirm
        title="确定删除该评论吗？"
        class="deleteComment"
        v-if="isAuthorself"
        @confirm="deleteComment(item)"
      >
        <el-button slot="reference" type="text">删除</el-button>
      </el-popconfirm>
    </div>
  </div>
</template>

<script>
import { deleteUserComment } from '@/api/article'
export default {
  name: 'CommentList',
  props: {
    comments: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      isAuthorself: true
    }
  },
  methods: {
    // 删除评论
    async deleteComment(item) {
      try {
        const res = await deleteUserComment(item._id)
        if (res.code === 200) {
          this.$message.success({
            message: '删除成功',
            offset: 80
          })
          this.$emit('updateCommentList')
        }
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.comment-list {
  padding: 0 20px 20px 20px;
  background-color: #fff;
  .list-item {
    position: relative;
    .deleteComment {
      display: none;
      position: absolute;
      top: 20px;
      right: 20px;
    }
    &:hover .deleteComment {
      display: block;
    }
  }
  .title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 20px;
  }
  .info {
    margin-left: 10px;
    a {
      font-size: 14px;
      color: #515767;
      text-decoration: none;
    }
    .content {
      line-height: 1.5;
      color: #333;
      margin-bottom: 5px;
      margin-top: 5px;
    }
  }
  .time {
    margin-top: 10px;
    margin-left: 40px;
    font-size: 12px;
    color: #999;
  }
}
</style>
